<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Message extends Controller
{
	private $_template_f;
	private $_lang;
	private $_template_id = 6;
	private $_num_item = 30;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->_template_f = $this->config->item('template_f');
		
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		
		
		$this->load->model('message/message_model');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Admarket administrator' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'message';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function send()
	{
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$uid = $this->session->userdata('uid');
		$data = array();
		
		
		if(isset($_POST['txtContent']))
		{
			$type = $this->mycommon->getSaveSqlStr($this->input->post('slcType'));
			$groupId = $this->mycommon->getSaveSqlStr($this->input->post('slcGroup'));
			
			$isMess = ($this->input->post('chkMess') == 'on') ? '1' : '';
			$isMail = ($this->input->post('chkMail') == 'on') ? '1' : '';
			$isUser = ($this->input->post('chkUser') == 'on') ? '1' : '';
			
			$title = trim($this->mycommon->getSaveSqlStr($this->input->post('txtTitle')));
			
			$isEditor = ($this->input->post('chkEditor') == 'on') ? '1' : '';
			$content = '';
			if($isEditor)
			{
				$content = trim($this->input->post('txtContent'));
			}
			else
			{
				$content = trim($this->mycommon->getSaveSqlStr($this->input->post('txtContent2')));
			}
			
						
			if($isUser)
			{
				$strUserId = trim($this->mycommon->getSaveSqlStr($this->input->post('hddSlcUser')));
				$lstUser = $this->message_model->mess_get_list_user_by_uid($strUserId);
				if(count($lstUser) > 0 && $content != '' && $title != '')
				{
					foreach($lstUser as $user)
					{
						if($isMess)
						{
							$this->message_model->inbox_add(0,$uid, $user['user_id'], $title, $content, 0, 0);
							usleep(50);
						}
						if($isMail)
						{
							$param = "[user_name]=";
							$param .= trim($user['contact_name']) != '' ? trim($user['contact_name']) : trim($user['username']);
							$param .= "$[mail_content]=" . $content;
							$this->message_model->mail_queue_add($this->_template_id, $param, $user['email_address'], '', $user['user_id']);
							usleep(50);
						}
					}
				}
			}
			else
			{
				if($type > -2 && $groupId >-2)
				{
					$lstUser = $this->message_model->get_list_message_user($type, $groupId);
										
					if(count($lstUser) > 0 && $content != '' && $title != '')
					{
						foreach($lstUser as $user)
						{
							if($isMess)
							{
								$this->message_model->inbox_add(0,$uid, $user['user_id'], $title, $content, 0, 0);
								usleep(50);
							}
							if($isMail)
							{
								$param = "[user_name]=";
								$param .= trim($user['contact_name']) != '' ? trim($user['contact_name']) : trim($user['username']);
								$param .= "$[mail_content]=" . $content;
								$this->message_model->mail_queue_add($this->_template_id, $param, $user['email_address'], '', $user['user_id']);
								usleep(50);
							}
						}
					}
				}
			}
			redirect(site_url('message/send'));
			die();
			
		}
		
		$this->_loadHeader('Gửi tin nhắn');
		
		$this->_loadMenu();
		
		$data['lstGroup'] = $this->message_model->get_all_group();
		
		$this->load->view($this->_template_f . 'message/send_view', $data);
		
		$this->_loadFooter();
	}
	
	function search()
	{
		if(!$this->session->userdata('uname'))
		{
			echo 'login';
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				echo 'notright';
			}
			else
			{
				$data = array();
				$type = $this->mycommon->getSaveSqlStr($this->input->get('type'));
				$groupId = $this->mycommon->getSaveSqlStr($this->input->get('g'));
				$sType = $this->mycommon->getSaveSqlStr($this->input->get('stype'));
				$keyword = $this->mycommon->getSaveSqlStr($this->input->get('keyword'));
				
				$cPage = $this->mycommon->getSaveSqlStr($this->input->get('cpage'));
				$cPage = (is_numeric($cPage) && $cPage > 0) ? $cPage : 1;
				
				$keyword_en = $this->myvietstring->removeVietChars($keyword);
				if($sType == 2)
				{
					$keyword_en = '+' . $keyword_en;
					$keyword = '+' . $keyword;
				}
				else
				{
					$keyword = strtolower($this->myvietstring->removeVietChars($keyword));
					$keyword =  '%' . $keyword . '%';
				}
										
				$lstUser = $this->message_model->mess_search_user($type, $groupId, $sType, $keyword, $keyword_en, $cPage, $this->_num_item);
				$data = $lstUser;
				$data['cPage'] = $cPage;
				$data['lstPaging'] = $this->mycommon->getListPaging($cPage, $data['pCount']);
				$this->load->view($this->_template_f . 'message/search_list_user_view', $data);
			}
		}				
	}
}